package com.rxst.msg.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.rxst.msg.base.ResponseVo;
import com.rxst.msg.dto.save.MsgxMenuSaveDto;
import com.rxst.msg.dto.save.menu.UpdateMenuAuthDto;
import com.rxst.msg.dto.search.MsgxMenuSearchDto;
import com.rxst.msg.dto.update.MsgxMenuUpdateDto;
import com.rxst.msg.entity.MsgxMenuEntity;
import com.rxst.msg.server.MsgxMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单表
 *
 * @author rxst
 * @email rxst@gmail.com
 * @date 2021-08-19 11:47:01
 */
@RestController
@RequestMapping("menu")
@Api(value = "菜单表", tags = "菜单表")
@Slf4j
public class MsgxMenuController {

    @Autowired
    private MsgxMenuService service;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseVo List(@RequestBody MsgxMenuSearchDto dto) {
        return ResponseVo.ok(service.list(dto));
    }

    /**
     * 分页Function
     */
    @PostMapping("/page")
    @ApiOperation("分页")
    public ResponseVo page(@RequestBody MsgxMenuSearchDto dto) {
        return ResponseVo.ok(service.page(dto));
    }

    /**
     * 详情
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseVo info(String id) {
        return ResponseVo.ok(service.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    public ResponseVo save(@RequestBody MsgxMenuSaveDto dto) {
        //校验类型
        MsgxMenuEntity entity = BeanUtil.toBean(dto, MsgxMenuEntity.class);
        return ResponseVo.ok(service.saveEntity(entity));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody MsgxMenuUpdateDto dto) {
        MsgxMenuEntity entity = BeanUtil.toBean(dto, MsgxMenuEntity.class);
        return ResponseVo.ok(service.updateEntity(entity));
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除")
    public ResponseVo delete(@RequestParam(value = "ids") List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            service.removeByIds(ids);
        } else {
            return ResponseVo.error("参数不能为空");
        }
        return ResponseVo.ok();
    }

    /**
     * 批量逻辑删除
     */
    @DeleteMapping("/delete/logic")
    @ApiOperation("批量逻辑删除")
    public ResponseVo deleteLogic(@RequestParam(value = "ids") List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            service.deleteLogicByIds(ids);
        } else {
            return ResponseVo.error("参数不能为空");
        }
        return ResponseVo.ok();
    }

    /**
     * 菜单树
     */
    @GetMapping("/tree")
    @ApiOperation("菜单树")
    public ResponseVo tree(String userId) {
        return ResponseVo.ok(service.tree(userId));
    }

    /**
     * 修改用户权限
     */
    @PostMapping("/update/auth")
    @ApiOperation("修改用户权限")
    public ResponseVo updateMenu(@RequestBody UpdateMenuAuthDto menuAuthDto) {
        service.updateMenuByUserId(menuAuthDto);
        return ResponseVo.ok();
    }
}
